package code.temp.log;

import java.util.Objects;
import java.util.stream.IntStream;

/**
 * <b>Description : </b>
 *
 * @author CPF
 * @date 2021/5/20 10:15
 **/
public class NumberPrint {

    private static final Object object = new Object();

    static class NumberPrintRun implements Runnable {

        public NumberPrintRun(int[] numbers) {
            Objects.requireNonNull(numbers);
            this.numbers = numbers;
        }

        private int[] numbers;

        @Override
        public void run() {
            if (numbers == null) {
                return;
            }
            synchronized (object) {
                for (int i = 0, len = numbers.length; i < len; i++) {

                    System.out.println(numbers[i]);
                    object.notify();
                    try {
                        if (i < len -1) {
                            object.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }


    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new NumberPrintRun(IntStream.range(0, 50).map(a -> a * 2 + 1).toArray()));
        Thread thread2 = new Thread(new NumberPrintRun(IntStream.range(1, 51).map(a -> a * 2).toArray()));
        thread1.start();
        thread2.start();
    }

}
